package com.changhai.diabetes.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.changhai.diabetes.model.LabPhysical;
import com.changhai.diabetes.model.LabSugar;
import com.changhai.diabetes.model.NutritionSuifang;
import com.changhai.diabetes.model.NutritionSurvey;
import com.changhai.diabetes.model.NutritionTreatment;
import com.changhai.diabetes.service.NutritionService;
import com.changhai.diabetes.util.Constants;

/**
 * 营养信息
 * 
 * @author Andy
 * 
 */
public class NutritionServiceImpl extends CommonServiceImpl implements NutritionService {

	public NutritionSurvey searchSurveyByPatientId(Long patientId) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "nutritionSurvey.getByPatientId", patientId);
		if (o != null && o instanceof NutritionSurvey) {
			return (NutritionSurvey) o;
		} else {
			NutritionSurvey s = new NutritionSurvey();
			s.setPatientid(patientId);
			return s;
		}
	}

	public Map<String, Object> searchTreatByPatientId(Long patientid) {
		Object oa = this.getObject(Constants.DS_CHANGHAI, "patient.getAge", patientid);
		Map<String, Object> result = new HashMap<String, Object>();
		NutritionTreatment treat = new NutritionTreatment();
		Integer recommend = 0;
		if (oa != null && oa instanceof Integer) {
			Integer age = (Integer) oa;
			Object ob = this.getObject(Constants.DS_CHANGHAI, "labPhysical.getHeightWeightAndStyle", patientid);
			if (ob != null && ob instanceof LabPhysical) {
				LabPhysical lp = (LabPhysical) ob;
				Object oc = this.getObject(Constants.DS_CHANGHAI, "sportsSurvey.getSportsType", patientid);
				if (oc != null && oc instanceof Integer) {
					Integer power = (Integer) oc;
					int level = this.calEnergyLevel(lp, age, power);
					if (level > 0) {
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("level", level);
						if (isHighSugar(patientid)) {// 高血糖
							recommend = 1;
						} else if (age >= 60 || power == 1) {// 老年人/轻体力
							recommend = 2;
						} else {// 普通
							recommend = 3;
						}
						map.put("flag", recommend);
						Object o = this.getObject(Constants.DS_CHANGHAI, "nutritionStandard.getByFlagAndLevel", map);
						if (o != null && o instanceof NutritionTreatment) {
							treat = (NutritionTreatment) o;
							if (suitFruit(patientid)) {
								treat.setSuitfruit(1);
							} else {
								treat.setSuitfruit(0);
							}

							// treat.setIdealweight(lp.getHeight() - 100 * 0.9);
							// // [身高（cm）—100]*0.9
							if (age >= 60 || power == 1) {
								treat.setLimitmeal(1);
							} else {
								treat.setLimitmeal(0);
							}
							treat.setPatientid(patientid);
							treat.setShuiguof(0.0);
							treat.setShuiguog(0.0);
							// 营养治疗开始时间
							DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
							treat.setStarttime(df.format(new Date()));

							Object ot = this.getObject(Constants.DS_CHANGHAI, "nutritionTreatment.getByPatientId",
									patientid);
							if (ot != null && ot instanceof NutritionTreatment) {
								NutritionTreatment t = (NutritionTreatment) ot;
								treat.setNutritreatid(t.getNutritreatid());
								this
										.updateObject(Constants.DS_CHANGHAI, "nutritionTreatment.updateByPrimaryKey",
												treat);
							} else {
								Integer id = this.insertObjectID(Constants.DS_CHANGHAI, "nutritionTreatment.insert",
										treat);
								if (id > 0) {
									treat.setNutritreatid(new Long(id));
								}
							}

							// 是否按照推荐饮食的食用量
							/*Object s = this.getObject(Constants.DS_CHANGHAI,
									"nutritionSurvey.getRecommAndTotalByPatientId", patientid);
							if (s != null && s instanceof NutritionSurvey) {
								NutritionSurvey survey = (NutritionSurvey) s;
								if (survey.getRecommend() == 1) {
									treat.setZhushig(survey.getZhushitotal());
									treat.setShucaig(survey.getShucaitotal());
									treat.setYuroug(survey.getYuroutotal());
									treat.setDoug(survey.getDoutotal());
									treat.setRug(survey.getRutotal());
									treat.setShuiguog(survey.getShuiguototal());
									treat.setYouzhik(survey.getYouzhitotal());
								}
							}*/
						}
					}
				}
			}
		}
		result.put("treat", treat);
		result.put("recommend", recommend);
		return result;
	}

	private boolean suitFruit(Long patientid) {
		Map map = new HashMap();
		map.put("patientid", patientid);
		map.put("testtime", 1);
		map.put("testmethod", 2);
		Object o = this.getObject(Constants.DS_CHANGHAI, "labSugar.getByTimeAndType", map);
		if (o != null && o instanceof LabSugar) {
			LabSugar sugar = (LabSugar) o;
			if (sugar.getSugar() < 7.0) {
				List<LabSugar> list = new ArrayList<LabSugar>();
				list = this.getList(Constants.DS_CHANGHAI, "labSugar.getBySpecType", map);
				if (list != null && list.size() > 0) {
					for (LabSugar s : list) {
						if (s.getSugar() != null && s.getSugar() >= 10.0) {
							return false;
						}
					}
					return true;// FPG<7mmol/L且2hPG<10mmol/L
				}
			}
		}
		return false;
	}

	private int calEnergyLevel(LabPhysical lp, Integer age, Integer power) {
		int level = 0;
		Double weight = lp.getWeight();
		Integer style = lp.getStyle();
		if (weight != null && style != null) {
			if (weight > 0 && style >= 0) {
				if (power != null && power >= 0) {
					int unit = 0;
					if (power == 0) {
						if (style == 0) {
							unit = 20;
						} else if (style == 1) {
							unit = 25;
						} else if (style == 2 || style == 3) {
							unit = 15;
						}
					} else if (power == 1) {
						if (style == 0) {
							unit = 30;
						} else if (style == 1) {
							unit = 35;
						} else if (style == 2 || style == 3) {
							unit = 25;
						}
					} else if (power == 2) {
						if (style == 0) {
							unit = 35;
						} else if (style == 1) {
							unit = 40;
						} else if (style == 2 || style == 3) {
							unit = 30;
						}
					} else if (power == 3) {
						if (style == 0) {
							unit = 40;
						} else if (style == 1) {
							unit = 45;
						} else if (style == 2 || style == 3) {
							unit = 35;
						}
					}
					/*能量供给量=劳动强度*理想体重（注：50岁以上者每增加10岁减少10%）
					  理想体重=身高（cm）—105 */		
					double amount = lp.getHeight()-105>0? (lp.getHeight()-105 * unit): 0;
					if (age != null && age >= 60) {
						amount = amount * (age - 50) / 10 * 0.9;
					}
					if (amount < 1200) {
						level = 1000;
					} else if (amount < 1400) {
						level = 1200;
					} else if (amount < 1600) {
						level = 1400;
					} else if (amount < 1800) {
						level = 1600;
					} else if (amount < 2000) {
						level = 1800;
					} else if (amount < 2200) {
						level = 2000;
					} else if (amount < 2400) {
						level = 2200;
					} else {
						level = 2400;
					}
				}
			}
		}
		return level;
	}

	private boolean isHighSugar(Long patientid) {
		Map map = new HashMap();
		map.put("patientid", patientid);
		map.put("testtime", 1);
		map.put("testmethod", 2);
		Object o = this.getObject(Constants.DS_CHANGHAI, "labSugar.getByTimeAndType", map);
		if (o != null && o instanceof LabSugar) {
			LabSugar sugar = (LabSugar) o;
			if (sugar.getSugar() >= 7.0) {
				return true;
			}
		}
		List<LabSugar> list = new ArrayList<LabSugar>();
		list = this.getList(Constants.DS_CHANGHAI, "labSugar.getByPatientIdAndType", map);
		if (list != null && list.size() > 0) {
			for (LabSugar s : list) {
				if (s.getSugar() < 11.1) {
					return false;
				}
			}
			return true;
		}
		return false;
	}

	public void saveEditNutritionSurvey(NutritionSurvey nutriSurvey) {
		if (nutriSurvey != null) {
			if (nutriSurvey.getNutrisurveyid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "nutritionSurvey.updateByPrimaryKey", nutriSurvey);
			} else {
				Long id = this.saveNewNutritionSurvey(nutriSurvey);
				if (id != null && id > 0) {
					nutriSurvey.setNutrisurveyid(id);
					this.updateObject(Constants.DS_CHANGHAI, "nutritionSurvey.updateByPrimaryKey", nutriSurvey);
				}
			}
		}
	}

	public Long saveNewNutritionSurvey(NutritionSurvey nutriSurvey) {
		Integer id = 0;
		if (nutriSurvey != null && nutriSurvey.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "nutritionSurvey.insert", nutriSurvey);
		}
		return new Long(id);
	}

	public void saveEditNutritionTreat(NutritionTreatment nutriTreat) {
		if (nutriTreat != null) {
			if (nutriTreat.getNutritreatid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "nutritionTreatment.updateByPrimaryKey", nutriTreat);
			} else {
				Long id = this.saveNewNutritionTreat(nutriTreat);
				if (id != null && id > 0) {
					nutriTreat.setNutritreatid(id);
					this.updateObject(Constants.DS_CHANGHAI, "nutritionTreatment.updateByPrimaryKey", nutriTreat);
				}
			}
		}
	}

	public void saveEditNutritionTreatTime(NutritionTreatment nutriTreat) {
		if (nutriTreat != null && nutriTreat.getPatientid() != null) {
			this.updateObject(Constants.DS_CHANGHAI, "nutritionTreatment.updateTimeByPatientId", nutriTreat);
		}
	}
	
	public Long saveNewNutritionTreat(NutritionTreatment nutriTreat) {
		Integer id = 0;
		if (nutriTreat != null && nutriTreat.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "nutritionTreatment.insert", nutriTreat);
		}
		return new Long(id);
	}
	
	public NutritionSuifang searchSuifangByPatientId(Long patientid){
		Object o = this.getObject(Constants.DS_CHANGHAI, "nutritionSuifang.getByPatientId", patientid);
		if (o != null && o instanceof NutritionSuifang) {
			return (NutritionSuifang) o;
		} else {
			NutritionSuifang s = new NutritionSuifang();
			s.setPatientid(patientid);
			return s;
		}
	}
	
	public void saveEditNutritionSuifang(NutritionSuifang nutriSuifang){
		if (nutriSuifang != null) {
			if (nutriSuifang.getSeqid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "nutritionSuifang.updateByPrimaryKey", nutriSuifang);
			} else {
				Long id = this.saveNewNutritionSuifang(nutriSuifang);
				if (id != null && id > 0) {
					nutriSuifang.setSeqid(id);
					this.updateObject(Constants.DS_CHANGHAI, "nutritionSuifang.updateByPrimaryKey", nutriSuifang);
				}
			}
		}
	}
	
	public Long saveNewNutritionSuifang(NutritionSuifang nutriSuifang){
		Integer id = 0;
		if (nutriSuifang != null && nutriSuifang.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "nutritionSuifang.insert", nutriSuifang);
		}
		return new Long(id);
	}
}
